package com.lulin.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.lulin.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * 支付宝支付的配置文件
 *
 * @author lulin
 * @date 2022/12/30 17:39
 */

//@ConfigurationProperties(prefix = "alipay") //如果在属性文件中配置，可以这些写配置
@Data
@Component
public class AlipayConfig {
    // 商户appid
    public static String APPID = "2021000122607121";
    // 自己的应用私钥 pkcs8格式的
    public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC0v7Z2l4vNywHBvMipsEkY8FSXnsba7y8knFJznWxeqQrWGjYcd4zZeeA6YdUD/1OLpJbKdz4mUfZXjCw98d5UYS8dzSiV8co3xGhJu2r1rxjOFxVDXHt79ZaW84tZz0RJa2BjOMaFA1COaLT91YZjf1jto3bTP03GY1j1j4ml2S076UBR420J0sio63zrFOQJDR3aaOXm5HQ0g7bQ3WUGw0NQ3d6T7OrvoBEAu/tVXTdN//Db4OrrjlxBfVqTIop+9XpESPA6JDSF+VWbbQ2pBTyC9TNPIRtBTUkRsSF5fjLkO1myxIC38JzfCYkgOxnWkRLL6TNyOAse3UwuDoxNAgMBAAECggEAVNlOYVuA0ImE+JqTpDzSA7gGLermNK5ldg1AE3AKCGHYaWuCjVh9sTWqUh3M7oR0hrAr7T2O2lRNjQmLV0TsdrXsdVPywnjv0cAMqNgnPNAGl/B0FdwbR7ShDZmkh/NpfgG/DLUBVD2bGk8C78nFvX1xlJTQ2hf0fbXzc8r+9fAzZPUKZo3eSde2UGJ5T4vwVzwaO3EZjMPs2OdmYAUu0CA1YkhagyVedl9kJ7QKv8mHeyTlw2tI2doI62XqIBD3qxtZp9vMlnXfqr6EtGsvCSoE1Swf+zovcosuwSx/7/pn4PcK5P5GrTKK55GcdWOyi5sXkyQ6sbCRy3u9bnRmoQKBgQDunFB38B7d5lf4S5bVNF3vx17QV44rnilUWcGXTi0OthuX7kpVNciRSkegBP1D56lVVG8lGl8PHZK7WDbD/neyUM54DGxtS6R1g5sgUeJl8Rv199YvE7x+FHcS8pr1BtdhgHHcYOQAVGSYsW19lUVzgkTu9MDaPghwGY3RTsCyJQKBgQDB6+PHOogITiAnXIAV8ZnwcaxtN++2suV/uU4kAj0up2cX2NrxKVqIS96S5ClF+J0QXBFzLLdraOgs9feELs619o8EQVGYNOZ+m1WEY3rELH+SdKVm18u9mLk1sa+5B5SjLezJ+3O/NaVLjDVWREUopwu/3kg6Zk9yUDJBvvpVCQKBgBWTYNtXhRL/jxjOjPb+YzgZSQOBx0M+a/JKo2yxDvXzlY8pZuNJl7c7hjp7+QK44eDrX5l1Rv8w52G1Wk5ipiQjlmfYh71iR56wpchIXzZklOiq3l0nZbXo19OIyGY7liIk4SbSRAublfUQphvi8YK+VslcrEuPrGemh+JTQq1ZAoGAATIHEW9W71Yr9JIpZ/k5TbYlrfauT54CtCrcRHSPE/6MMCVyFBKBMF3nXVhyuK+zqCopIWChMNRQuhY6FGKFe7GtaAVPD3UkBpvGhy5lz1fG4Yw+iMfDxcNvQNl7j9RwIiFiWExohkE6Z6DRo1qAYqPE+sXQBCwlSTsdVDJtMKECgYEA4o0vdvP+WK69Z0qhttB293do43jG986l68VkLgStlClFn5dsLVMDbQf7AE+Kw+9nO1LV5rIkOlmXPnS8ivUo6ezkrpvFPjkoURjVQFBk+HQ4zHGRrbKaQz9/hespc9TGzZB+qwxXDNTon93iUavpnoTEqycaCIpy1H0xKPBr60o=";
    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //public static String notify_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/notify_url.jsp";
    public static String notify_url = "http://order.lulin.com/orderPay/payed/notifyUrl";//异步通知操作方法
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    //public static String return_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/return_url.jsp";
    public static String return_url = "http://order.lulin.com/orderPay/returnUrl";//支付后回调的的操作
    // 请求网关地址
    public static String URL = "https://openapi.alipaydev.com/gateway.do";
    // 编码
    public static String CHARSET = "UTF-8";
    // 返回格式
    public static String FORMAT = "json";
    // 支付宝公钥
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiMxpGIRaw7jYBzfVaq0a7Kq+PHwOcCdW/5LZZ4wuJuadNO5LV28FzIIgN57IiWxNLUkeTZPAHokBnUMsaTzbQ7RCvoJIyCbgWmZQbfNOw4UJ7BX7j6Y8QqBxi2cvRm8sV98tdra9F08YFd5opE+dHR1heRx35tKDp9K8jg9AwlARda+hzZ8iHH1H3QcDkFb09TotNzpvHpZw8o0r9SFlK7T/9ImRf+aBV77h8/s4mj/H0X7jp75Xptv7oO9VGoRqSO3JFJJ6poo5CW5dPKQNmufHzt/s0R8k0ZRBY3ROLSixloeIxCKc2d6VI4XZsCqDYxjQbpzzxyOARCcEcfZxuwIDAQAB";
    // 日志记录目录
    public static String log_path = "/log";
    // RSA2
    public static String SIGNTYPE = "RSA2";

    /**
     * 支付的方法
     *
     * @param payVo vo数据对象
     */
    public String pay(PayVo payVo) {
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(URL, APPID, RSA_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request = new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(payVo.getOut_trade_no());
        model.setSubject(payVo.getSubject());
        model.setTotalAmount(payVo.getTotal_amount());
        model.setBody(payVo.getBody());
        model.setTimeoutExpress("5000");//超时时间
        model.setProductCode("111111");//商品编号-可以有很多个，先固定
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(return_url);

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            return form;
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
